<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="renderer" content="webkit" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>千峰教育大前端Vue教学</title>
  <!-- 引入vue -->
  <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
</head>

<body style="margin:100px;font-size:22px;">
  <!-- 定义一个容器 -->
  <div id="app">
    <!-- 声明模板内容 -->
    <div v-if="leftTime>0">还有{{leftTime}}分钟下课</div>
    <div>{{state}}</div>
    <hr>
    <div>当前值：{{num}}</div>
    <button @click="changeNum">更改数据</button>
    <div v-for="(item, key) in log" :key="key">
      {{item}}
    </div>
  </div>
  <script>
    // 创建应用
    const app = Vue.createApp({
      // 初始化数据函数
      data() { // 返回一个对象，对象中写初始化数据
        return {
          time: 0, // 初始化一个的数据
          state: '上课中...',
          num: 1,
          log: [], // 变化的过程
        };
      },
      computed: {
        leftTime() {
          return 10 - this.time;
        },
      },
      watch: { // 侦听器配置
        time(cur) { // 监听一个叫time的数据
          console.log('=================watch time');
          if (cur >= 10) {
            this.state = '下课了';
            clearInterval(this.timerId);
          }
        },
        num(cur, pre) {
          this.log.push(`从 ${pre} 变成了 ${cur}`);
        }
      },
      created() { // 生命周期
        // this.lastNum = this.num;
        this.timerId = setInterval(() => { this.time++ }, 1000);
      },
      methods: {
        changeNum() {
          this.num = Math.round(Math.random() * 100);
          // this.log.push(`从 ${this.lastNum} 变成了 ${this.num}`);
          // this.lastNum = this.num;
        }
      },
    });
    // 挂载应用
    app.mount('#app');
  </script>
</body>

</html>